package com.avic.modules.login.rest;

import com.avic.dozer.service.IGenerator;
import com.avic.modules.login.service.LoginLogService;
import com.avic.modules.login.service.dto.LoginLogDto;
import com.avic.modules.login.service.dto.LoginLogQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhangzikang
 * @date 2022-08-14
 */
@AllArgsConstructor
@Api(tags = "用户: 登录日志")
@RestController
@RequestMapping("/api/login_log")
public class LoginLogController {

    private final LoginLogService loginLogService;
    private final IGenerator generator;

    @ApiOperation("查看登录日志")
    @GetMapping
    @PreAuthorize("@el.check('admin','login_log:list')")
    public ResponseEntity<Object> getAlarmRecords(LoginLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(loginLogService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @ApiOperation("导出登录日志数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','login_log:download')")
    public void download(LoginLogQueryCriteria criteria,HttpServletResponse response,Pageable pageable) throws IOException {
        loginLogService.download(generator.convert(loginLogService.queryAllS(criteria,pageable), LoginLogDto.class),response);
    }
}
